package com.yazi.mxz.fieldsale.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.fieldsale.entity.beans.StoreGoods;
import com.yazi.mxz.fieldsale.repository.StoreGoodsRepository;

@Controller("storeGoodsController")
@RequestMapping("/storeGoods")
public class StoreGoodsController extends BaseController {
	
	@Resource
	StoreGoodsRepository storeGoodsRepository;
	
	@RequestMapping("/code/json")
	@ResponseBody
	public ObjectResult<?> codeJson(String barCode, Pageable pageable) {
		ObjectResult<List<StoreGoods>> jsonResult = new ObjectResult<List<StoreGoods>>();
		Page<StoreGoods> page;
		try {
			if (barCode.length() == 13) {
				long codeNum = Long.parseLong(barCode);
				if (codeNum > 2e12 && codeNum < 29e11) {
					barCode = barCode.substring(2, 7);// 打秤类
				}
				page = storeGoodsRepository.findByBarCode(barCode, pageable);
			} else {
				page = storeGoodsRepository.findByBarCodeLike(barCode, pageable);
			}
			jsonResult.setResult(page.getContent());
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
}
